package com.zxy.service.impl;


import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zxy.util.Constant;
import com.zxy.mapper.CourseMapper;
import com.zxy.pojo.dto.CourseAdDTO;
import com.zxy.pojo.dto.PageDTOCourseDTO;
import com.zxy.pojo.entity.Course;
import com.zxy.pojo.vo.CourseContractResponseVO;
import com.zxy.service.CourseService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {
    @Autowired
    private CourseMapper courseMapper;

    //分页查询数据
    @Override
    public CourseContractResponseVO<Course> pagequery(PageDTOCourseDTO dto) {
            Page<Course> page = new Page<>(dto.getPageNum(), dto.getPageSize());
            Page<Course> pages = courseMapper.pagequery(page, dto);
            CourseContractResponseVO<Course> courseContractResponseVO = new CourseContractResponseVO<>();
            courseContractResponseVO.setTotal(pages.getTotal());
            courseContractResponseVO.setRows(pages.getRecords());
            courseContractResponseVO.setCode(Constant.SUCCESS);
            courseContractResponseVO.setMsg(Constant.MSG);
            courseContractResponseVO.setParams(Constant.CONTRACTSTATUS);
            return courseContractResponseVO;
    }

    //添加课程
    @Override
    public void saveOne(CourseAdDTO dto) {
        Course course = new Course();
        BeanUtils.copyProperties(dto, course);
        course.setSubjectId(Integer.valueOf(dto.getSubject()));
        course.setPrice(dto.getPrice());
        String code = RandomUtil.randomString(8);//生成8位随机数字字符串
        course.setCode(code);
        courseMapper.insert(course);
    }
}
